<?php
/**
 * Created by PhpStorm.
 * User: HouJie
 * Date: 2018-4-3
 * Time: 17:19
 */

namespace Bqdove\Cache;

use Bqdove\Contracts\CacheInterface;
use Predis\Client;

class RedisCache extends CacheInterface
{
    /**
     * RedisCache constructor.
     * @param array $config
     * @throws \Exception
     */
    public function __construct(array $config)
    {
        if (!is_null($config)) {
            $this->config = array_merge($this->config, $config);
        }

        $this->app = new Client($this->config);
    }

    /**
     * 获取指定key的值
     * @param string $key
     * @param null $default
     * @return bool|mixed|string
     */
    public function get($key, $default = null)
    {
        $value = $this->app->get(md5($this->getKey($key)));
        if ($value !== false) {
            return $this->unserialize($value);
        }

        return $this->value($value);
    }

    /**
     * 获取并删除
     * @param $key
     * @return bool|mixed|string
     */
    public function pull($key)
    {
        $value = $this->get($key);
        $this->delete($key);
        return $value;
    }

    /**
     * 永久存储
     * @param $key
     * @param $value
     * @return bool|mixed
     */
    public function forever($key, $value)
    {
        return $this->set($key, $value, 0);
    }

    /**
     * 自增
     * @param $key
     * @param int $value
     * @return bool|int|mixed|string
     */
    public function increment($key, $value = 1)
    {
        if ($this->has($key)) {
            $value = $this->get($key) + $value;
        }
        return $this->set($key, $value, $this->config['expired']) ? $value : false;
    }

    /**
     * 自减
     * @param $key
     * @param int $value
     * @return bool|int|mixed|string
     */
    public function decrement($key, $value = 1)
    {
        if ($this->has($key)) {
            $value = $this->get($key) - $value;
        }
        return $this->set($key, $value) ? $value : false;
    }

    /**
     * 设置，存在则覆盖
     * @param string $key
     * @param mixed $value
     * @param null $ttl
     * @return bool
     */
    public function set($key, $value, $ttl = null)
    {
        $ttl = is_null($ttl) ? $this->config['expired'] : $ttl * 60;
        $response = $this->app->set(md5($this->getKey($key)), $this->serialize($this->value($value)), 'EX', $ttl);
        if ($response->getPayload() == 'OK') {
            return true;
        }
        return false;
    }


    /**
     * 删除指定的key
     * @param string $key
     * @return bool|int
     */
    public function delete($key)
    {
        return $this->app->del(md5($this->getKey($key))) ? true : false;
    }

    /**
     * 清除所有数据
     * @return bool|mixed
     */
    public function clear()
    {
        return $this->app->flushAll()->getPayload() == 'OK' ? true : false;
    }

    public function getMultiple($keys, $default = null)
    {
        // TODO: Implement getMultiple() method.
    }

    public function setMultiple($values, $ttl = null)
    {
        // TODO: Implement setMultiple() method.
    }

    public function deleteMultiple($keys)
    {
        // TODO: Implement deleteMultiple() method.
    }

    /**
     * 判断是否存在
     * @param string $key
     * @return bool
     */
    public function has($key)
    {
        if ($this->app->exists(md5($this->getKey($key)))) return true;
        return false;
    }

    /**
     * 返回缓存的key
     * @param $key
     * @return string
     */
    private function getKey($key)
    {
        return $this->config['preFix'] . $key;
    }


}